package org.daochong.fastdev.funcs;

import java.sql.SQLException;

import org.daochong.fastdev.Context;
import org.daochong.fastdev.Function;
import org.daochong.lang.BeanUtil;
import org.daochong.lang.StringUtils;

public class IfNullFunction implements Function {

	@Override
	public boolean is(String id) {
		return "ifNull".equals(id);
	}

	@Override
	public void exec(String key, String prop, Context context) throws SQLException {
		if (!is(key) || StringUtils.isEmpty(prop) || context == null)
			return;
		String[] ar = prop.split(",");
		String act = ar[0].trim();
		Object o = context.searchValue(act);
		if (o == null) {
			for (int i = 1; i < ar.length; i++) {
				o = context.searchValue(ar[i].trim());
				if (o != null) {
					BeanUtil.setValue(context, act, o);
					break;
				}
			}
		}
	}

}
